package com.shujia.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 基于HBase中的'stu'表，统计班级人数 最终结果写入HDFS
 */
public class Demo05MRReadHBase {
    // Map任务
    public static class MyMapper extends TableMapper<Text, IntWritable> {
        @Override
        protected void map(ImmutableBytesWritable key, Result value, Mapper<ImmutableBytesWritable, Result, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            // key 对应Rowkey  value 对应一条数据
            String clazz = Bytes.toString(value.getValue("info".getBytes(), "clazz".getBytes()));
            context.write(new Text(clazz), new IntWritable(1));
        }
    }

    // Reduce任务
    public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int cnt = 0;
            for (IntWritable value : values) {
                cnt += value.get();
            }
            context.write(key, new IntWritable(cnt));
        }
    }

    // Driver端
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://master:9000");

        Job job = Job.getInstance();

        // 设置预聚合combiner类
        job.setCombinerClass(MyReducer.class);

        job.setJobName("Demo05MRReadHBase");
        job.setJarByClass(Demo05MRReadHBase.class);

        Scan scan = new Scan();

        // 配置Map任务
        // 由于处理的是HBase中的数据 故HBase提供了一个工具 可以对Map任务进行配置
        TableMapReduceUtil.initTableMapperJob(
                "stu"
                ,scan
                ,MyMapper.class
                ,Text.class
                ,IntWritable.class
                ,job
        );


        // 配置Reduce任务
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 判断输出路径是否存在，存在就删除
        FileSystem fs = FileSystem.get(conf);
        Path outputPath = new Path("/hbaseMR/output01");
        if (fs.exists(outputPath)) {
            fs.delete(outputPath, true);
        }
        // 配置输入输出路径
        FileOutputFormat.setOutputPath(job, new Path("/hbaseMR/output01"));

        job.waitForCompletion(true);

    }


}
